package com.zqwh.xtool.model.other;

import com.zqwh.xtool.model.LockModel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class LockModelLatchImpl implements LockModel {
    public LockModelLatchImpl() {
        try {
            FileOutputStream fos = new FileOutputStream("/sys/class/gpio/export");
            fos.write("233".getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            FileOutputStream fos = new FileOutputStream("/sys/class/gpio/gpio233/direction");
            fos.write("in".getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void lock() {
    }

    @Override
    public void unlock() {
    }

    @Override
    public int getDoorStatus() {
        FileInputStream fis = null;
        String ret = "0";
        try {
            byte[] buffer = new byte[64];
            fis = new FileInputStream("/sys/class/gpio/gpio233/value");
            if (fis.available() > 0) {
                int len = fis.read(buffer, 0, buffer.length);
                if (len > 0) {
                    ret = new String(buffer, 0, len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return ret.startsWith("0") ?0:1;
    }

    @Override
    public void getDoorStatus(ReadDoorStatusListener listener) {

    }

    @Override
    public void lightOn(int number) {

    }

    @Override
    public void lightOff(int number) {

    }

    @Override
    public void lightOnAll() {

    }

    @Override
    public void lightOffAll() {

    }
}
